<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;

use app\admin\model\Archive as Arc;

use think\Request;
use think\Loader;
use think\Url;

class Product extends AdminBase
{
	public function index()
	{
		return 'test';
	}

	public function add()
	{
		$pageConfig = [];
		$pageConfig['up_url'] = Url::build('create');
		$this->assign('cateData',Loader::model('Category')->getCateTree());
		$this->assign('pageConfig',json_encode($pageConfig));

		return $this->fetch();
	}

	public function edit($id = 0)
	{

		//主题数据获取
		$data = Arc::with('category,product')->find($id);

		$pageConfig = [];
		$pageConfig['up_url'] = Url::build('editSave');

		$this->assign('cateData',Loader::model('Category')->getCateTree());
		$this->assign('pageConfig',json_encode($pageConfig));
		$this->assign('data',$data);
		return $this->fetch('form');
	}
	public function editSave($id)
	{
		$data = $this->request->param();

		$arc = Arc::get($id,'product');

		$validate = Loader::validate('article');

		if(!$validate->scene('edit')->check($data)){
			$this->error($validate->getError());
		}

		$arc->allowField(true)->data($data);


		//这里最后需要修改成自动属性?
		$arc->product->order_year = $this->request->param('order_year');
		$arc->product->content 	= $data['content'];

/*		//分开保存
		dump($arc->save());
		dump($arc->article->save());

		//共同保存
		//$arc->together('article')->save();*/
		
		if(!$arc->together('product')->save())
		{
			return $this->error(($arc->getError()?$arc->getError():'没有修改内容'),'index');
		}

		Loader::model('BackstageLog')->record("修改产品页面,文档ID:[{$id}]");

		return $this->success('产品页面修改成功', Url::build('index'));
	}
}